"""
    Module with final document description and accompanying classes.
"""

from datetime import date
from sqlobject.col import UnicodeCol, ForeignKey, IntCol, DateCol, DecimalCol
from mnemoply.model.model_object import ModelObject
from mnemoply.model.hub import __connection__

class DocumentType(ModelObject):
    """
        Dictionary class.
        Contains min works and min hours for creation document with this type.
    """

    model_title = _("Document type")

    name = UnicodeCol(length=80, notNull=True, title=_("Name"))
    minHours = IntCol(notNull=True, title=_("Minimum hours"))
    minWorks = IntCol(notNull=True, title=_("Minimum works"))
    comment = UnicodeCol(default='', notNull=True, title=_("Comment"))

    title = property(lambda self: self.name)

class Qualification(ModelObject):
    """
        Dictionary class.
    """

    model_title = _("Qualification")

    name = UnicodeCol(length=80, notNull=True, alternateID=True, title=_("Name"))
    comment = UnicodeCol(default='', notNull=True, title=_("Comment"))
    title = property(lambda self: self.name)

class Specialization(ModelObject):
    """
        Dictionary class.
    """

    model_title = _("Specialization")

    name = UnicodeCol(length=80, notNull=True, alternateID=True, title=_("Name"))
    comment = UnicodeCol(default='', notNull=True, title=_("Comment"))
    title = property(lambda self: self.name)

class FinalDocument(ModelObject):
    """
        Document for student. We create it after student education finished.
    """

    model_title = _("Final document")

    student = ForeignKey('Student', notNull=True, title=_("Student"))
    yearBegin = IntCol(default=None, title=_("Start year"))
    yearEnd = IntCol(default=None, title=_("End year"))
    hours = DecimalCol(notNull=True, size=5, precision=1, title=_("Hours"), default=0)
    works = IntCol(notNull=True, title=_("Works amount"), default=0)
    owner = ForeignKey('Person', notNull=True, title=_("Owner"))
    lastTopic = ForeignKey('FinalWorkTopic', default=None, title=_("Last topic"))
    creationDate = DateCol(notNull=True, default=lambda: date.today(), title=_("Creation date"))
    defenceDate = DateCol(notNull=True, default=lambda: date.today(), title=_("Defence date"))
    number = IntCol(notNull=True, title=_("Number"))

    organization = ForeignKey('Organization', notNull=True, default=0, title=_("Organization"))

    documentType = ForeignKey('DocumentType', default=None, title=_("Document type"))
    qualification = ForeignKey('Qualification', default=None, title=_("Qualification"))
    specialization = ForeignKey('Specialization', default=None, title=_("Specialization"))

    def getTitle(self):
        if self.documentType:
            return "%s %s" % (self.student.title, self.documentType.title)
        return self.student.title

    title = property(lambda self: self.getTitle())
